import { tools, ajax } from './ajax.js';

//#region 发送手机验证码的部分
let txt_imageCode = document.getElementById('txt_imageCode');
let img_code = document.getElementById('img_code');
let spImg = document.getElementById('spImg');
let txt_phone = document.getElementById('txt_phone');
let spPhone = document.getElementById('spPhone');

// 获取图片验证码
function getImgCode() {
  ajax.post('/tool/getImageCode', {}, (data) => {
    if (data.success) {
      img_code.setAttribute('src', data.message);
    } else {
      showToast('获取图片验证码失败');
    }
  });
}

img_code.addEventListener('click', getImgCode);
spImg.addEventListener('click', getImgCode);

getImgCode();

// 发送手机验证码
spPhone.addEventListener('click', () => {
  ajax.post(
    '/tool/sendValidateCode',
    {
      imageCode: txt_imageCode.value,
      phone: txt_phone.value,
    },
    (data) => {
      showToast(data.message);
    }
  );
});

//#endregion

//#region 加密/解密的部分
let txt_info = document.getElementById('txt_info');
let spEn = document.getElementById('spEn');
let spDe = document.getElementById('spDe');
let txt_result = document.getElementById('txt_result');

spEn.addEventListener('click', () => {
  ajax.post(
    '/tool/encrypt',
    {
      info: txt_info.value,
    },
    (data) => {
      txt_result.value = data.message;
    }
  );
});

spDe.addEventListener('click', () => {
  ajax.post(
    '/tool/decrypt',
    {
      info: txt_info.value,
    },
    (data) => {
      txt_result.value = data.message;
    }
  );
});

//#endregion

//#region 信息弹出层的部分
let divToast = document.getElementById('divToast');
// querySelector是通过css选择器获取页面元素（只会返回一个）
let toastBody = document.querySelector('#divToast .toast-body');
// 创建toast对象
const toast = new bootstrap.Toast(divToast);

// 显示toast对话框
function showToast(message) {
  // 修改内容
  toastBody.innerHTML = message;
  // 显示
  toast.show();
}
//#endregion
